package Q701;

/**
 * 二叉搜索树中的插入操作
 * @author 23737
 * @time 2021.12.11
 */
public class Test {
    public static void main(String[] args) {

    }
}


 class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

/**
 * 递归
 */
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        return helper(root, val);
    }
    public TreeNode helper(TreeNode root, int val){
        if(root == null){
            return new TreeNode(val);
        }
        if(root.val < val){
            root.right = helper(root.right, val);
        }else{
            root.left = helper(root.left, val);
        }
        return root;
    }
}
